import {Body, Controller, Delete, Get, Param, Patch, Post, Req, UseGuards,} from "@nestjs/common";
import {JwtAuthGuard} from "../auth/guards/jwt-auth.guard";
import {TaskService} from "../task/task.service";
import {RespWrap} from "src/common/base.utils";
import {UpdateTaskDto} from "./dto/update.task.dto";
import {CreateTaskDto} from "./dto/create.task.dto";
import {getRepository} from "typeorm";
import {TaskEntity} from "./task.entity";

@UseGuards(JwtAuthGuard)
@Controller("tasks")
export class TaskController {
  constructor(private taskService: TaskService) {}


  @Get(':id')
  async get(@Param() params) {
    return RespWrap(await getRepository(TaskEntity).findOne({id: params.id}));
  }

  @Get()
  async pageQuery(@Req() req) {
    const resp = await this.taskService.pageQuery({
      ...req.query,
    });
    return RespWrap(resp);
  }

  @Post()
  async insert(@Body() body: CreateTaskDto, @Req() req) {
    body.userId = req.user.id;
    const resp = await this.taskService.insert(body);
    return RespWrap(resp);
  }

  @Delete(":id")
  async removeById(@Param() params) {
    const resp = await this.taskService.removeById(params.id);
    return RespWrap(resp);
  }

  @Patch(":id")
  async update(@Param() params, @Body() body: UpdateTaskDto) {
    const resp = await this.taskService.update({
      id: params.id,
      ...body,
    });
    return RespWrap(resp);
  }
}
